﻿function submitEmailform(form, formID, messageID)
{ 
    var sYourname = "";
    var sYourlastname = "";
    var sYourfirstname = "";
    var sYouremail = "";
    var sYourmessage = "";
    var sStockcode = "";
    var sEmailType = "";
    var sProductName = "";

    var sMatrix_id = "";
    var sCompetitors_Website = "";
    var sPricetoBeat = "";
    var sFriendName = "";
    var sFriendEmail = "";
    var sTelephone = "";
    var sFormEyeDee = "";
    var sPageRequestID = $("#pageRequestId").val();

    if (typeof form.formEyeDee != 'undefined') sFormEyeDee = form.formEyeDee.value;
    if (typeof form.name != 'undefined') sYourname = form.name.value;
    if (typeof form.lastname != 'undefined') sYourlastname = form.lastname.value;
    if (typeof form.firstname != 'undefined') sYourfirstname = form.firstname.value;
    if (typeof form.email != 'undefined') sYouremail = form.email.value;
    if (typeof form.comment != 'undefined') sYourmessage = form.comment.value;
    if (typeof form.stockcode != 'undefined') sStockcode = form.stockcode.value;
    if (typeof form.matrix_id != 'undefined') sMatrix_id = form.matrix_id.value;
    if (typeof form.Competitors_Website != 'undefined') sCompetitors_Website = form.Competitors_Website.value;
    if (typeof form.PricetoBeat != 'undefined') sPricetoBeat = form.PricetoBeat.value;
    if (typeof form.friendname != 'undefined') sFriendName = form.friendname.value;
    if (typeof form.friendemail != 'undefined') sFriendEmail = form.friendemail.value;
    if (typeof form.telephone != 'undefined') sTelephone = form.telephone.value;
    if (typeof form.productname != 'undefined') sProductName = form.productname.value;
    sEmailType = form.emailtype.value;

    var posturl = "/ajax/sendemail";
    var postdata = '';
    
    postdata += "pageRequestId=" + sPageRequestID;
    postdata += "&vp_formeyedee=" + sFormEyeDee;
    postdata += "&vp_name=" + sYourname;
    postdata += "&vp_firstname=" + sYourfirstname;
    postdata += "&vp_lastname=" + sYourlastname;
    postdata += "&vp_email=" + sYouremail;
    postdata += "&vp_comment=" + sYourmessage;
    postdata += "&vp_stockcode=" + sStockcode;
    postdata += "&vp_EmailType=" + sEmailType;
    postdata += "&vp_Matrix_id=" + sMatrix_id;
    postdata += "&vp_CompetitorWebsite=" + sCompetitors_Website;
    postdata += "&vp_PricetoBeat=" + sPricetoBeat;
    postdata += "&vp_FriendName=" + sFriendName;
    postdata += "&vp_FriendEmail=" + sFriendEmail;
    postdata += "&vp_Telephone=" + sTelephone;
    postdata += "&vp_productname=" + sProductName;
    postdata = encodeURI(postdata);

    $.ajax({
        type: "POST",
        url: posturl,
        data: postdata,
        async: false,
        success: function (msg) {
            //alert("email send: " + msg);
            if (msg == "ok") {
                $("#" + messageID ).show();
                $("#" + formID ).hide();
            }
            else {
                alert("oops... something went wrong. Please try again.");
            }
        }
    });
}
function submitForm(form, formID, messageID, actionName) {
    var sYourname = "";
    var sYourlastname = "";
    var sYourfirstname = "";
    var sYouremail = "";
    var sYourmessage = "";
    var sStockcode = "";
    var sFormType = "";
    var sCustomerTown = "";
    var sPurchaseDate = "";
    var sRating = "";
    var sFormEyeDee = "";

    var sWishListTypeID = "";
    var sWishListID = "";
    var sWishListName = "";
    var sSubmitVisitorRef = "";
    var sSubmitCustomerID = "";

    var axis1 = "";
    var axis2 = "";
    var axis3 = "";
    var axis4 = "";
    var axis5 = "";

    var sPageRequestID = $("#pageRequestId").val();

    if (typeof form.formEyeDee != 'undefined') sFormEyeDee = form.formEyeDee.value;
    if (typeof form.name != 'undefined') sYourname = form.name.value;
    if (typeof form.lastname != 'undefined') sYourlastname = form.lastname.value;
    if (typeof form.firstname != 'undefined') sYourfirstname = form.firstname.value;
    if (typeof form.email != 'undefined') sYouremail = form.email.value;
    if (typeof form.comment != 'undefined') sYourmessage = form.comment.value;
    if (typeof form.stockcode != 'undefined') sStockcode = form.stockcode.value;
    if (typeof form.CustomerTown != 'undefined') sCustomerTown = form.CustomerTown.value;
    if (typeof form.PurchaseDate!= 'undefined') sPurchaseDate = form.PurchaseDate.value;
    if (typeof form.rating != 'undefined') sRating = form.rating.value;
    if (typeof form.selectWishListType != 'undefined') sWishListTypeID = form.selectWishListType.value;
    if (typeof form.selectWishList != 'undefined') sWishListID = form.selectWishList.value;
    if (typeof form.addWishListType != 'undefined') sWishListTypeID = form.addWishListType.value;
    if (typeof form.addWishListName != 'undefined') sWishListName = form.addWishListName.value;
    if (typeof form.axis1 != 'undefined') sRating = form.axis1.value;
    if (typeof form.axis2 != 'undefined') sRating = form.axis2.value;
    if (typeof form.axis3 != 'undefined') sRating = form.axis3.value;
    if (typeof form.axis4 != 'undefined') sRating = form.axis4.value;
    if (typeof form.axis5 != 'undefined') sRating = form.axis5.value;
    

    sFormType = form.formtype.value;

    var posturl = "/ajax/form";
    var postdata = '';

    postdata += "pageRequestId=" + sPageRequestID;
    postdata += "&vp_formeyedee=" + sFormEyeDee;
    postdata += "&vp_name=" + sYourname;
    postdata += "&vp_firstname=" + sYourfirstname;
    postdata += "&vp_lastname=" + sYourlastname;
    postdata += "&vp_email=" + sYouremail;
    postdata += "&vp_comment=" + sYourmessage;
    postdata += "&vp_stockcode=" + sStockcode;
    postdata += "&vp_formType=" + sFormType;
    postdata += "&vp_CustomerTown=" + sCustomerTown;
    postdata += "&vp_PurchaseDate=" + sPurchaseDate;
    postdata += "&vp_Rating=" + sRating;
    postdata += "&vp_WishListTypeID=" + sWishListTypeID;
    postdata += "&vp_WishListID=" + sWishListID;
    postdata += "&vp_WishListName=" + sWishListName;
    postdata += "&vp_axis1=" + axis1;
    postdata += "&vp_axis2=" + axis2;
    postdata += "&vp_axis3=" + axis3;
    postdata += "&vp_axis4=" + axis4;
    postdata += "&vp_axis5=" + axis5;
    postdata += "&vp_formID=" + formID;
    
    postdata = encodeURI(postdata);

    $.ajax({
        type: "POST",
        url: posturl,
        data: postdata,
        async: false,
        success: function (data) {
            var splitbits = data.split("¬");

            if (splitbits[0] == "ok") {
                switch (actionName)
                {
                    case 'thankyou':
                        $("#" + messageID).show();
                        $("#" + formID).hide();
                        break;
                    case 'replace':
                        $("#" + formID).empty();
                        $("#" + formID).append(splitbits[1]);
       
                        if (messageID.length > 0)
                        { $("#" + messageID).show(); }
                        break;
                    default:
                        break;
                }
            }
            else {
                alert("oops... something went wrong. Please try again.");
            }
        }
    });
}
function verifyWishListAddForm(form)
{
    var returnval = true;
    var message = "";
    if (!validateString(form.addWishListName.value, "")) {
        message += ("Please give a valid wishlist name\n");
        returnval = false;
    }
    if (!validateNumber(form.addWishListType.value, "-1")) {
        message += ("Please select a wishlist type\n");
        returnval = false;
    }
    if (returnval) {

        submitForm(form, "selectWishList", "wishListAddProductForm", "replace");
        form.addWishListName.value = "";
    }
    else {
        alert(message);
    }
    return false;
}
function verfiyWishListAddProductForm(form)
{
    var returnval = true;
    var message = "";
    if (!validateNumber(form.selectWishList.value, "-1")) {
        message += ("Please select a wishlist name\n");
        returnval = false;
    }
    if (!validateNumber(form.selectWishListType.value, "-1")) {
        message += ("Please select a wishlist type\n");
        returnval = false;
    }
    if (returnval)
    {
        submitForm(form, "wishListContents", "wishlistThankyou", "thankyou");
        parent.$("#wishlistAddLink").hide();
        parent.$("#wishlistStatus").html("In your wishlist");
    }
    else {
        alert(message);
    }
    return false;
}
function askProductFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.name.value, "")) {
        message += ("Please give a valid name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (!validateString(form.comment.value, "Type your comment here")) {
        message += ("Please write a message\n");
        returnval = false;
    }
    if (!validateObscentiy(form.comment.value)) {
        message += ("Your comment seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval) {
        submitEmailform(form, "commentContents", "commentThankyou");
    } else {
        alert(message);
    }
    return false;
}
function contactFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.name.value, "")) {
        message += ("Please give a valid name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (!validateString(form.comment.value, "Type your comment here")) {
        message += ("Please write a message\n");
        returnval = false;
    }
    if (!validateObscentiy(form.comment.value)) {
        message += ("Your comment seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval) { submitEmailform(form, "commentContents", "commentThankyou"); }
    else {
        alert(message);
    }
    return false;
}
function reviewProductFormCheck() {
    
    var returnval = true;
    var message = "";
    var form = document.getElementById('reviewProductForm');
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.comment.value, "")) {
        message += ("Please give a valid review\n");
        returnval = false;
    }
    if (typeof form.name != 'undefined')
    {
        if (!validateString(form.name.value, "")) {
            message += ("Please give a valid name\n");
            returnval = false;
        }
    }
    if (typeof form.lastname != 'undefined')
    {
        if (!validateString(form.lastname.value, "")) {
            message += ("Please give a valid last name\n");
            returnval = false;
        }
    }
    if (typeof form.firstname != 'undefined')
    { 
        if (!validateString(form.firstname.value, "")) {
            message += ("Please give a valid first name\n");
            returnval = false;
        }
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (!validateString(form.PurchaseDate.value, "")) {
        message += ("Please give a valid purchase date\n");
        returnval = false;
    }
    if (!validateString(form.CustomerTown.value, "")) {
        message += ("Please give a valid town\n");
        form.CustomerTown.focus();
        returnval = false;
    }
    if (!validateString(form.rating.value, "")) {
        message += ("Please select your star rating\n");
        form.rating.focus();
        returnval = false;
    }
    
    if (!validateObscentiy(form.comment.value.value)) {
        message += ("Your review seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        form.comment.focus();
        returnval = false;
    }
    if (returnval) {
        submitForm(form, "reviewContents", "reviewThankyou", "thankyou");
    } else {
        alert(message);
    }
    return false;
}
function rmaFormCheck(form) {
    var returnval = true;
    var message = "";
    if (!validateNumber(form.ReturnQty.value, "0")) {
        message += ("Please give a quantity you wish to return\n");
        returnval = false;
    }
    if (!parseInt(form.ReturnQty.value) > parseInt(form.maxQty.value)) {
        message += ("The maximum quantity you can return is " + form.maxQty.value + "\n");
        returnval = false;
    }
    if (!validateString(form.returnreason.value, "0")) {
        message += ("Please select the reason for returning the product\n");
        returnval = false;
    }
    if (!validateString(form.comment.value, "")) {
        message += ("Please write some details about the reason for returning the product\n");
        returnval = false;
    }
    if (!validateObscentiy(form.comment.value)) {
        message += ("Your comment seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval) {
        var posturl = "/ajax/form";
        var postdata = '';
        postdata += "vp_EmailType=returnrequest";
        postdata += "&vp_OrderID=" + document.emailform.orderid.value;
        postdata += "&vp_StockCode=" + document.emailform.stockcode.value;
        postdata += "&vp_ReturnQty=" + document.emailform.ReturnQty.value;
        postdata += "&vp_comment=" + document.emailform.comment.value;
        postdata += "&vp_returnreason=" + document.emailform.returnreason.value;
        var x = document.emailform.returnreason.selectedIndex;
        var y = document.emailform.returnreason.options;
        postdata += "&vp_returnreasonDesc=" + y[x].text;
        postdata = encodeURI(postdata);

        $.ajax({
            type: "POST",
            url: posturl,
            data: postdata,
            dataType: "json",
            success: function (msg) {
                //alert("email send: " + msg);
                if (msg == "ok") {
                    $("#rmaThankyou").show();
                    $("#rmaContents").hide();
                }
                else {
                    alert(msg);
                }
            }
        });
    }
    else {
        alert(message);
    }
    return false;
}
function form_checkNewsletter(form) {
    var returnval = true;
    var message = "";
    if (!validateString(form.firstname.value, "")) {
        message += ("Please enter your firstname\n");
        returnval = false;
    }
    if (!validateString(form.lastname.value, "")) {
        message += ("Please enter your lastname\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (returnval) {
        submitForm(form, "subscribeContents", "subscribeThankyou", "thankyou");
    } else {
        alert(message);
    }
    return false;
}
function seenbetterFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    } 
    if (!validateString(form.name.value, "")){
        message += ("Please give a valid name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address.");
            returnval = false;
        }
    }
    if (!validateString(form.Competitors_Website, "")) {
        message += ("Please give a valid website name\n");
        returnval = false;
    }
    if (!validateString(form.PricetoBeat.value,"")) {
        message += ("Please give a valid price to beat\n");
        returnval = false;
    }
    if (!validateString(form.comment.value,"Type your comment here")) {
        message += ("Please write a message\n");
        returnval = false;
    }
    if (!validateObscentiy(form.comment.value)) {
        message += ("Your comment seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval)
    {
        submitEmailform(form, "commentContents", "commentThankyou");
    } else {
        alert(message);
    }
    return false;
}
function tellfriendProductFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.name.value, "")) {
        message += ("Please give a valid name\n");
        returnval = false;
    }
    if (!validateString(form.friendname.value,"")) {
        message += ("Please give a valid friend name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (!validateString(form.friendemail.value, "")) {
        message += ("Please give a valid friend email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.friendemail.value)) {
            message += ("Invalid friend email address\n");
            returnval = false;
        }
    }
    if (!validateString(form.comment.value, "Type your comment here")) {
        message += ("Please write a message\n");
        returnval = false;
    }
    if (!validateObscentiy(form.comment.value)) {
        message += ("Your comment seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval) {
        submitEmailform(form, "commentContents", "commentThankyou");
    } else {
        alert(message);
    }
    return false;
}
function notifyProductFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.name.value, "")) {
        message += ("Please give a valid name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (returnval) {
        submitEmailform(form, "commentContents", "commentThankyou");
    } else {
        alert(message);
    }
    return false;
}
function suggestProductFormCheck(form) {
    var returnval = true;
    var message = "";
    if (typeof form.formEyeDee != 'undefined') {
        if (!validatePolitely(form.formEyeDee.value)) {
            message += ("Please validate you are human\n");
            returnval = false;
        }
    }
    if (!validateString(form.comment.value, "")) {
        message += ("Please give a valid suggested product\n");
        returnval = false;
    }
    if (!validateString(form.firstname.value, "")) {
        message += ("Please give a valid first name\n");
        returnval = false;
    }
    if (!validateString(form.lastname.value,"")) {
        message += ("Please give a valid last name\n");
        returnval = false;
    }
    if (!validateString(form.email.value, "")) {
        message += ("Please give a valid email address\n");
        returnval = false;
    }
    else {
        if (!validateEmail(form.email.value)) {
            message += ("Invalid email address\n");
            returnval = false;
        }
    }
    if (!validateObscentiy(form.comment.value.value)) {
        message += ("Your suggestion seems to contain inappropriate words. No adult words are allowed. Please correct and re-submit\n");
        returnval = false;
    }
    if (returnval) {
        submitEmailform(form, "commentContents", "commentThankyou");
    } else {
        alert(message);
    }
    return false;
}r

function validateObscentiy(stringVal) {
    var filter = /bitch|fuck|cunt|arse|wank|pussy|pussie|pussies|vagina|punkass|shit|asshole|fucker|nigger|dike/i;
    if (filter.test(stringVal))
    { return false; }
    else { return true; }
}
function validateString(stringVal, emptyValue) {
    if (stringVal.match(/^s+$/) || stringVal == emptyValue || stringVal == "")
    { return false; }
    else { return true; }
}
function validateNumber(stringVal, initialValue) {
    if (stringVal.match(/^[^0-9]+$/) || stringVal == initialValue || stringVal == "")
    { return false; }
    else { return true; }
}
function validateEmail(stringVal) {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (!filter.test(stringVal))
    { return false; }
    else { return true; }
}